package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.usermapper.MemberMapper;
import com.example.fitnessservice.module.pojo.MemberPurchaseOrder;
import com.example.fitnessservice.module.pojo.UserMember;
import com.example.fitnessservice.module.utils.TimeUtils;
import com.example.fitnessservice.service.MemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper ourMapper;

    @Override
    public UserMember findVipInformationById(Integer userId) throws ParseException {
        UserMember userMember = ourMapper.findVipInformationById(userId);
        System.out.println(userMember);

        //查到会员记录之后，要判断用户会员是否过期
        //会员状态码为1的时候，是没有过期的
        if(userMember != null && userMember.getMemberStatus() == 1){
            updateMemberStatus(userMember);
        }

        return userMember;
    }

    @Override
    public boolean memberRenewal(MemberPurchaseOrder memberPurchaseOrder) {
        //将订单信息插入到用户会员订单表中
        int count1 = ourMapper.addMemberOrder(memberPurchaseOrder);

        //现在查询用户是否有称为会员的记录
        UserMember record = ourMapper.findVipInformationById(memberPurchaseOrder.getUserId());
        int count2 = 0;
        //更新用户会员表中的信息
        if(record != null){
            //用户不是第一次开通会员
            if(record.getMemberStatus() == 1){
                //用户的会员没有过期，只用修改用户的会员截止时间
                record.setEndTime(updateMemberEndTime(memberPurchaseOrder, record));
            }else {
                //用户的会员过期了，除了修改用户的会员截止时间，还要修改用户的会员开始时间
                record.setStartTime(memberPurchaseOrder.getOrderTime());
                record.setEndTime(updateMemberEndTime(memberPurchaseOrder, record));
            }
            //将更新后的数据放回到数据库
            count2 = ourMapper.updateMemberTime(record);
        }else {
            //用户是第一次开会员，要在会员表中新增一条记录
            UserMember userMember = new UserMember();
            userMember.setUserId(memberPurchaseOrder.getUserId());
            userMember.setStartTime(memberPurchaseOrder.getOrderTime());
            userMember.setEndTime(updateMemberEndTime(memberPurchaseOrder, record));
            userMember.setMemberStatus(1);
            count2 = ourMapper.addMember(userMember);
        }

        return (count1 == 1 && count2 ==1) ? true : false;
    }

    //修改用户会员的结束日期
    private Date updateMemberEndTime(MemberPurchaseOrder memberPurchaseOrder, UserMember record) {
        Calendar calendar = Calendar.getInstance();
        if(memberPurchaseOrder.getMemberType() == 1){
            //月卡加30天
            calendar.add(Calendar.DATE, 30);
        }else if(memberPurchaseOrder.getMemberType() == 2){
            //季卡加90天
            calendar.add(Calendar.DATE, 90);
        }else {
            //年卡加365天
            calendar.add(Calendar.DATE, 365);
        }
        return calendar.getTime();
    }


    //在对会员信息进行操作前判断会员信息是否过期
    public void updateMemberStatus(UserMember userMember) throws ParseException {
        //获取当前时间的Date类型数据
        Date now = TimeUtils.getNowTime();

        //当前时间和会员的截止日期比较，判断会员是否过期
        //当compare为-1，没有过期；当compare为1或0时，会员过期
        int compare = now.compareTo(userMember.getEndTime());
        if(compare != -1){
            //会员过期，要对数据库中的数据进行修改，根据用户的id修改
            ourMapper.updateMemberStatus(userMember.getUserId());
            //将查询到的数据中用户会员的状态变为0
            userMember.setMemberStatus(0);
        }
    }
}
